package com.som.service.excel.data1.process;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.som.bean.data1.input.IpAddressAllInfo;
import com.som.bean.data1.input.ResourceControInfo;
import com.som.bean.data1.output.IpCompData;
import com.som.bean.data1.output.IpaddressJoin;
import com.som.console.ExcelProcException;
import com.som.console.PathEnum;
import com.som.service.excel.ExcelConvDataProcessInf;
import com.som.service.excel.ExcelReadProcess;
import com.som.service.excel.ExcelReadWriteProcess;
import com.som.service.excel.data1.input.IpAddressAllExcelRead;
import com.som.service.excel.data1.input.ResourceControlExcelRead;
import com.som.service.excel.data1.output.IpaddressJoinExcelWrite;

/**
 * 进行设备采集数据和资管系统数据比对
 * 
 * 
 * @author liujun
 * @date 2016年4月8日
 * @verion 0.0.1
 */
public class DevCompResDataProcess implements ExcelConvDataProcessInf<IpCompData> {

    /**
     * 输入数据一：ip地址总表(输入文件名为input1.xls)
     */
    private ExcelReadProcess<String, IpAddressAllInfo> ipAddressReadInput1 = new IpAddressAllExcelRead();

    /**
     * 输入数据四：资管系统数据（输入的文件名为input4.xls）
     */
    private ExcelReadProcess<String, ResourceControInfo> resourceControlInput4 = new ResourceControlExcelRead();

    /**
     * 输出文件1处理：以bookback关联
     */
    private ExcelConvDataProcessInf<IpaddressJoin> neAreaJoinOutput1Process = new IpaddressJoinProcess();

    /**
     * 输出文件1：通过LOOPBACK地址让数据二与数据三关联
     */
    private ExcelReadWriteProcess<IpaddressJoin> neAreaJoinOutput = new IpaddressJoinExcelWrite();

    /**
     * @param pathMap
     * @return
     * @throws ExcelProcException
     */
    public Map<String, IpCompData> convProcessData(Map<String, String> pathMap) throws ExcelProcException {
        // 1,输出表格一:
        // 通过LOOPBACK地址让数据二与数据三关联
        // 以设备采集的数据为准
        Map<String, IpaddressJoin> ipaddressJoinMap = neAreaJoinOutput1Process.convProcessData(pathMap);

        // 将输出表格1写入到指定文件目录
        neAreaJoinOutput.readWriteExcelByMap(pathMap.get(PathEnum.DATA1_OUTPUT_NEAREAJOIN.getPathFlag()), 0, true,
                ipaddressJoinMap);

        // 读取所有输入文件
        // 输入数据一：ip地址总表(输入文件名为input1.xls)
        Map<String, IpAddressAllInfo> input1IPaddressallMap = ipAddressReadInput1
                .readExcelDataMap(pathMap.get(PathEnum.DATA1_INPUT_IPADDRESSALL.getPathFlag()), 0, true);
        // 输入数据四：资管系统数据（输入的文件名为input4.xls）
        Map<String, ResourceControInfo> input4ResourceControlMap = resourceControlInput4
                .readExcelDataMap(pathMap.get(PathEnum.DATA1_INPUT_RESOUCECONTROL.getPathFlag()), 0, true);

        // 进行数据的对比操作
        if (null != input1IPaddressallMap && !input1IPaddressallMap.isEmpty()) {
            Map<String, IpCompData> result = new LinkedHashMap<String, IpCompData>();

            Iterator<Entry<String, IpAddressAllInfo>> ipaddrIter = input1IPaddressallMap.entrySet().iterator();

            IpCompData ipCompData = null;
            IpaddressJoin ipjoinBean = null;
            ResourceControInfo resControlbean = null;

            String ip = null;

            Entry<String, IpAddressAllInfo> next = null;
            while (ipaddrIter.hasNext()) {
                next = ipaddrIter.next();
                ip = next.getKey();

                ipCompData = new IpCompData();
                ipCompData.setIpaddress(ip);

                // 进行设备采集数据对应区县信息提取
                ipjoinBean = ipaddressJoinMap.get(ip);

                if (null != ipjoinBean) {
                    ipCompData.setNeMask(ipjoinBean.getNetwork());
                    ipCompData.setNeName(ipjoinBean.getNeInfo());
                    ipCompData.setArea(ipjoinBean.getAreainfo());
                }

                // 设置资管每天数据
                resControlbean = input4ResourceControlMap.get(ip);

                if (null != resControlbean) {
                    ipCompData.setResUseNe(resControlbean.getUseNe());
                    ipCompData.setResourceUser(resControlbean.getGroupUserName());
                }

                // 检查设备与资管网元是否一致（是和否）
                if (null != ipCompData.getNeName() && null != ipCompData.getResUseNe()) {
                    ipCompData.setNeName(ipCompData.getNeName().trim());
                    ipCompData.setResUseNe(ipCompData.getResUseNe().trim());
                    if (ipCompData.getNeName().equals(ipCompData.getResUseNe())) {
                        ipCompData.setCompRsp("是");
                    } else {
                        ipCompData.setCompRsp("否");
                    }
                } else {
                    ipCompData.setCompRsp("否");
                }

                result.put(ip, ipCompData);
            }

            return result;
        }

        return null;
    }

}
